package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.stm.entity.StmStockInout;
import com.flyco.modules.stm.entity.StmStockInoutItem;
import com.flyco.modules.stm.mapper.StmStockInoutItemMapper;
import com.flyco.modules.stm.mapper.StmStockInoutMapper;
import com.flyco.modules.stm.service.IStmStockInoutService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 总仓出入库单
 * @Author: flyco
 * @Date:   2020-12-03
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmStockInoutServiceImpl extends ServiceImpl<StmStockInoutMapper, StmStockInout> implements IStmStockInoutService {

	@Autowired
	private StmStockInoutMapper stmStockInoutMapper;
	@Autowired
	private StmStockInoutItemMapper stmStockInoutItemMapper;
	
	@Override
	@Transactional
	public void saveMain(StmStockInout stmStockInout, List<StmStockInoutItem> stmStockInoutItemList) {
		stmStockInoutMapper.insert(stmStockInout);
		for(StmStockInoutItem entity:stmStockInoutItemList) {
			//外键设置
			entity.setInoutId(stmStockInout.getId());
			stmStockInoutItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmStockInout stmStockInout,List<StmStockInoutItem> stmStockInoutItemList) {
		stmStockInoutMapper.updateById(stmStockInout);
		
		//1.先删除子表数据
		stmStockInoutItemMapper.deleteByMainId(stmStockInout.getId());
		
		//2.子表数据重新插入
		for(StmStockInoutItem entity:stmStockInoutItemList) {
			//外键设置
			entity.setInoutId(stmStockInout.getId());
			stmStockInoutItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmStockInoutItemMapper.deleteByMainId(id);
		stmStockInoutMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmStockInoutItemMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmStockInoutMapper.deleteById(id);
		}
	}
	
}
